Research
Security News
Malicious npm Packages Inject SSH Backdoors via Typosquatted Libraries
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.
The read-pkg npm package is used to read a package.json file and parse its contents. It can normalize the data according to the npm normalization rules, handle different file encodings, and can read from different locations by resolving the path to the nearest package.json file.
Read package.json from the current directory
This feature reads the package.json file from the current working directory and outputs the parsed JSON object.
const readPkg = require('read-pkg');
(async () => {
const pkg = await readPkg();
console.log(pkg);
})();
Read package.json from a specified directory
This feature allows you to specify a directory from which to read the package.json file, rather than the current working directory.
const readPkg = require('read-pkg');
(async () => {
const pkg = await readPkg({ cwd: 'path/to/directory' });
console.log(pkg);
})();
Read and normalize package.json data
This feature reads the package.json file and normalizes its data according to npm normalization rules, which can be useful for ensuring consistent data structure.
const readPkg = require('read-pkg');
(async () => {
const pkg = await readPkg({ normalize: true });
console.log(pkg);
})();
read-package-json is an npm package that reads the package.json file and also provides normalization. It is the library that npm itself uses to read package.json files, which means it's well-tested and reliable, but it might be more complex and tightly coupled with npm's internal use cases compared to read-pkg.
load-json-file is a package that provides a simple API for reading and parsing JSON files. It does not specifically target package.json files, so it lacks the normalization features of read-pkg, but it can be used for general JSON file reading purposes.
pkg-conf is a package that reads configuration from package.json files. It's focused on retrieving configuration settings rather than reading the entire package.json file, so it serves a slightly different purpose compared to read-pkg.
Read a package.json file
npm install read-pkg
import {readPackage} from 'read-pkg';
console.log(await readPackage());
//=> {name: 'read-pkg', …}
console.log(await readPackage({cwd: 'some-other-directory'}));
//=> {name: 'unicorn', …}
Returns a Promise<object>
with the parsed JSON.
Returns the parsed JSON.
Type: object
Type: URL | string
Default: process.cwd()
Current working directory.
Type: boolean
Default: true
Normalize the package data.
Parses an object or string into JSON.
Type: object | string
An object or a stringified object to be parsed as a package.json.
Type: object
Type: boolean
Default: true
Normalize the package data.
package.json
fileFAQs
Read a package.json file
The npm package read-pkg receives a total of 39,367,270 weekly downloads. As such, read-pkg popularity was classified as popular.
We found that read-pkg demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Research
Security News
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.
Security News
MITRE's 2024 CWE Top 25 highlights critical software vulnerabilities like XSS, SQL Injection, and CSRF, reflecting shifts due to a refined ranking methodology.
Security News
In this segment of the Risky Business podcast, Feross Aboukhadijeh and Patrick Gray discuss the challenges of tracking malware discovered in open source softare.